package org.springblade.core.boot.config;

import com.alibaba.druid.wall.WallFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

	@Autowired
	WallFilter wallFilter;

	@Value("${spring.datasource.type}")
	private Class<? extends DataSource> datasourceType;

	@Bean(name = "dataSource", destroyMethod = "close")
	@Primary
	@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
	public DataSource dataSource() {
		return DataSourceBuilder.create().type(datasourceType).build();
	}

	@Bean(name = "wallFilter")
	@ConfigurationProperties(prefix = "spring.datasource.druid.filters.wall")
	public WallFilter wallFilter() {
		return new WallFilter();
	}

}
